package com.str.LLs;
public class SLLOps {
	
  private SLLNode head;
    
  public SLLOps(SLLNode link) { head = link; }
    
  public void insert(Object val, SLLNode prior) {
    // Insert val at a given point in this SLL, either after the node 
    // prev, or before the head node if prev is null.
    SLLNode ins = new SLLNode(val);
    if (prior == null) {         
      ins.next = head;
      head = ins;
    } 
    else {
      ins.next = prior.next;
      prior.next = ins;
    }
  }

  public void delete(SLLNode del) {
    // Delete node del from this SLL.
    if (del == head)
      head = head.next; 
    else {
      SLLNode prior = head;
      while (prior.next != del)
        prior = prior.next;
      prior.next = del.next;
    }
  }

  public SLLNode search(Object val) {
    // Find which (if any) node of this SLL contains an element equal to 
    // target. Return a link to the matching node (or null if there is 
    // none).
    for (SLLNode curr = head; curr != null; curr = curr.next)
      if (val.equals(curr.data))
        return curr;
    return null;
  }

  public void print() {
    // Print the contents of the SLL as a String
    System.out.print("head");
    for (SLLNode curr = head; curr != null; curr = curr.next)
      System.out.print(" -> " + curr.data);
    System.out.println(" -> null");
  }
    
  public static void main(String args[]) {
    String[] word = {"fox", "cow", "pig", "cat", "rat", "lio",
                     "tig", "goa", "dog"};

    SLLNode first = null, temp;
    for (int i=0; i<word.length; i++) {
      temp = new SLLNode(word[i], first);
      first = temp;
    }

    SLLOps sll = new SLLOps(first);

    System.out.print("Initially SLL: ");
    sll.print();

    temp = sll.search("tig");
    sll.insert("ant", temp);
    System.out.print("insert \"ant\" after \"tig\": ");
    sll.print();
        
    temp = sll.search("cat");
    sll.insert("eag", temp);
    System.out.print("insert \"eag\" after \"cat\": ");
    sll.print();

    temp = sll.search("eag");        
    sll.delete(temp);
    System.out.print("delete value \"eag\": ");
    sll.print();                
  }
}
